import cv2
from string import ascii_uppercase as AtoZ
from openpyxl import load_workbook
from openpyxl.styles import PatternFill
def aaa():
    # 读取图片
    #image = cv2.imread("potato.png")
    image = cv2.imread("abc.jpg")
    height, width = image.shape[:2]
    if width>625:
        rate=width/625.0
        image = cv2.resize(image, (625,int(height/rate) ), interpolation=cv2.INTER_CUBIC)
    # 载入Excel
    wb = load_workbook(filename='test.xlsx')
    # type:np.ndarray
    # 活动的sheet获取
    ws = wb.active

    # 字母列表生成
    # col_name = ['A', 'B', ...., 'Z']
    col_name = list(AtoZ)

    # excel列名生成
    for i in range(len(image[0]) - 26):
        col_name.append(AtoZ[i // 26] + AtoZ[i % 26])



    for row in range(len(image)):


        for col in range((int)(len(image[row])/4)):
            # 红，绿，蓝的值转16进制,并将结果转为字符串
            red, green, blue = image[row][col*4]
            color = '%02x%02x%02x' % (blue, green, red)
            # 要填充的单元格名获取
            cell_name = col_name[col] + str(row + 1)
            # 设置单元格名
            cell = ws[cell_name]
            # 填充颜色
            cell.fill = PatternFill(patternType='solid', fgColor=color)

    # excel保存
    wb.save('test.xlsx')
    print('文件已生成')












'''
import cv2
from string import ascii_uppercase as AtoZ
from openpyxl import load_workbook
from openpyxl.styles import PatternFill

# 读取图片
image = cv2.imread("img.png")

# 载入Excel
wb = load_workbook(filename='test.xlsx')
# type:np.ndarray
# 活动的sheet获取
ws = wb.active

# 字母列表生成
# col_name = ['A', 'B', ...., 'Z']
col_name = list(AtoZ)

# excel列名生成
for i in range(len(image[0]) - 26):
    col_name.append(AtoZ[i // 26] + AtoZ[i % 26])

for col in range(len(image[0])):
    # 列的宽度调整
    ws.column_dimensions[col_name[col]].width = 0.3

for row in range(len(image)):
    # 行的高度调整
    ws.row_dimensions[row].height = 1.5

    for col in range(len(image[row])):
        # 红，绿，蓝的值转16进制,并将结果转为字符串
        red, green, blue = image[row][col]
        color = '%02x%02x%02x' % (blue, green, red)

        # 要填充的单元格名获取
        cell_name = col_name[col] + str(row + 1)
        # 设置单元格名
        cell = ws[cell_name]
        # 填充颜色
        cell.fill = PatternFill(patternType='solid', fgColor=color)

# excel保存
wb.save('test.xlsx')

'''